//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.persistence;

import java.sql.*;
import java.util.Vector;

// Referenced classes of package org.jpgolay.me.persistence:
//            ConnectDB
public class SQLList {

    public SQLList() {
    }

    public boolean search(String request) {
        select(request);
        return result;
    }

    public Vector getRows() {
        return rows;
    }

    public Object[][] getRowsData() {
        return data;
    }

    public Vector getColumns() {
        return columns;
    }

    public ResultSet getResultSet() {
        return rs;
    }

    private void select(String requete) {
        Connection con = null;
        PreparedStatement ps = null;
        rs = null;
        try {
            con = getConnection();
            ps = con.prepareStatement(requete);
            rs = ps.executeQuery();
            if (rs.next()) {
                rows = new Vector();
                columns = new Vector();
                ResultSetMetaData rsmd = rs.getMetaData();
                int numcols = rsmd.getColumnCount();
                for (int i = 1; i <= numcols; i++) {
                    columns.addElement(rsmd.getColumnName(i));
                }

                int r = 0;
                do {
                    for (int i = 1; i <= numcols; i++) {
                        rows.addElement(rs.getString(i));
                    }

                    r++;
                } while (rs.next());
                data = new Object[r][numcols];
                r = 0;
                int j = 0;
                for (int i = 0; i < rows.size(); i++) {
                    data[r][j] = rows.elementAt(i);
                    if (++j == numcols) {
                        j = 0;
                        r++;
                    }
                }

                result = true;
            } else {
                result = false;
            }
        } catch (SQLException sql) {
            result = false;
        } finally {
            try {
                ps.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return;
    }

    private Connection getConnection() {
        return ConnectDB.getConnection();
    }
    private boolean result;
    private Vector rows;
    private Object data[][];
    private Vector columns;
    private ResultSet rs;
}
